package com.example.service.serviceImp;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.dao.UserMapper;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.util.ValueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
@Service("UserService")
public class UserServiceImp implements UserService {


    /**
     * 添加用户
     *
     * @param json
     **/

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImp(UserMapper userMapper) {
        this.userMapper = userMapper;

    }

    @Override
    public Object addUser(JSONObject json) {
        String phone=json.getString("phone");
        if (ValueUtil.isMobile(phone)){
            User user = JSONObject.parseObject(json.toJSONString(), User.class);
            userMapper.insert(user);
            return 1;
        }else
            return 0;

    }

    /**
     * 登录验证
     *
     * @param json
     **/
    @Override
    public Object login(JSONObject json) {
        User user=JSONObject.parseObject(json.toJSONString(),User.class);
        String phone=user.getPhone();
        String password=user.getPassword();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone).eq("password",password);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 更新用户信息
     *
     * @param json
     **/
    @Override
    public Object update(JSONObject json,User user) {
        //User user=JSONObject.parseObject(json.toJSONString(),User.class);
       /* String nickname=json.getString("nickname");
        String sign= json.getString("sign");
        String avatar= json.getString("avatar");
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("nickname",nickname).eq("sign",sign).eq("avatar",avatar);
        return userMapper.update(user,userUpdateWrapper);*/
        return null;
    }
    // @Override
//    public Object selectBymp(String mobile,String password){
//
//        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("name",mobile).eq("password",password);
//        return userMapper.selectOne(queryWrapper);
//    }

    public Object getUser(HttpServletRequest req){
        User user = null;
        Integer userId;
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("user")) {
                String values = cookie.getValue();
                // 如果value字段不为空
                if (StringUtils.isNotBlank(values)) {
                    userId= Integer.parseInt(values);
                    user  = (User)userMapper.selectById(userId);
//                    String[] elements = values.split("-");
//                     获取账户名或者密码
//                    if (StringUtils.isNotBlank(elements[0])) {
//                        mobile = elements[0];
//                    }
//                    if (StringUtils.isNotBlank(elements[1])) {
//                        password = elements[1];
                }

            }
        }



        return user;
    }

    public Object find(JSONObject json){
        String phone=json.getString("phone");
        String newPassword=json.getString("newPassword");
        User user=new User();
        user.setPassword(newPassword);
        UpdateWrapper<User> wrapper=new UpdateWrapper<>();
        wrapper.eq("phone",phone);
        userMapper.update(user,wrapper);
        return 1;
    }
}
